package cn.iocoder.yudao.module.javainfo.dal.mysql.financialflow;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.javainfo.controller.admin.financialflow.vo.FinancialFlowPageReqVO;
import cn.iocoder.yudao.module.javainfo.dal.dataobject.financialflow.FinancialFlowDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 资金流水 Mapper
 *
 * @author 爪哇国大BOSS
 */
@Mapper
public interface FinancialFlowMapper extends BaseMapperX<FinancialFlowDO> {

    default PageResult<FinancialFlowDO> selectPage(FinancialFlowPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<FinancialFlowDO>()
                .eqIfPresent(FinancialFlowDO::getUserId, reqVO.getUserId())
                .eqIfPresent(FinancialFlowDO::getPetId, reqVO.getPetId())
                .eqIfPresent(FinancialFlowDO::getOrderNumber, reqVO.getOrderNumber())
                .eqIfPresent(FinancialFlowDO::getAmountType, reqVO.getAmountType())
                .eqIfPresent(FinancialFlowDO::getTransactionChannel, reqVO.getTransactionChannel())
                .eqIfPresent(FinancialFlowDO::getAmount, reqVO.getAmount())
                .eqIfPresent(FinancialFlowDO::getBalanceAfter, reqVO.getBalanceAfter())
                .eqIfPresent(FinancialFlowDO::getBusinessType, reqVO.getBusinessType())
                .eqIfPresent(FinancialFlowDO::getRelatedId, reqVO.getRelatedId())
                .eqIfPresent(FinancialFlowDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(FinancialFlowDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(FinancialFlowDO::getFlowId));
    }

    List<FinancialFlowDO> selectListByUserIds(@Param("userIds") List<Long> userIds);

    /**
     * 获取指定用户的最新资金流水记录
     *
     * @param userId 用户ID
     * @return 最新的资金流水记录
     */
    default FinancialFlowDO selectLatestByUserId(Long userId) {
        return selectOne(new LambdaQueryWrapperX<FinancialFlowDO>()
                .eq(FinancialFlowDO::getUserId, userId)
                .orderByDesc(FinancialFlowDO::getCreateTime)
                .last("limit 1"));
    }

    /**
     * 获取最近x天每天的资金流水统计
     *
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param amountType 金额类型
     * @return 每日统计列表
     */
    List<Map<String, Object>> selectDailyStatistics(@Param("startDate") LocalDate startDate,
                                                   @Param("endDate") LocalDate endDate,
                                                   @Param("amountType") String amountType);

    /**
     * 获取最近x天用户资金流水排行榜
     *
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param amountType 金额类型
     * @param limit 限制数量
     * @return 用户排行榜
     */
    List<Map<String, Object>> selectUserRank(@Param("startDate") LocalDate startDate,
                                            @Param("endDate") LocalDate endDate,
                                            @Param("amountType") String amountType,
                                            @Param("limit") Integer limit);

    /**
     * 获取用户在指定时间范围内的消费统计（按宠物分组）
     *
     * @param userId 用户ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param amountType 金额类型
     * @return 宠物消费统计列表
     */
    List<Map<String, Object>> selectUserPetConsumptionStats(@Param("userId") Long userId,
                                                          @Param("startTime") LocalDateTime startTime,
                                                          @Param("endTime") LocalDateTime endTime,
                                                          @Param("amountType") String amountType);
}